<?php

namespace Usap\Service\Common;

class alarms
{
    /**
     * @param
     * 获取警报务列表
     */

    public function getAlarmsList($where, $page = 1, $rows = '', $order, $sort, $files = '*')
    {
        $model = D("Usap/Common/Alarms");
        $orders = $order . ' ' . $sort;
        $count = $model->alias('a')->join('left join `devices` as b on a.codeserial = b.codeserial left join `user` as c on a.user_id = c.user_id')->where($where)->count();
        if ($count > 0) {
            if ($rows) {
                $list = $model->field('a.*,b.device_address,c.user_name,b.device_category,b.alarms_times,b.manager_phone')->alias('a')->join('left join devices as b on a.codeserial = b.codeserial left join user as c on a.user_id = c.user_id')->where($where)->page($page, $rows)->order($orders)->select();
            } else {
                $list = $model->field('a.*,b.device_address,c.user_name,b.device_category,b.alarms_times,b.manager_phone')->alias('a')->join('left join devices as b on a.codeserial = b.codeserial left join user as c on a.user_id = c.user_id')->where($where)->select();
            }
        } else {
            $count = 0;
            $list = [];
        }
        return array('count' => $count, 'list' => $list);
    }

    /**
     * 获取对应小时的警报汇总
     */
    public function getAlarmsByHour()
    {
//        $date = '2018-11-01';
        $time = strtotime(date('Y-m-d 00:00:00'));
//        $time = strtotime($date);
        $where = " created_time >'$time'";
        $model = D("Usap/Common/Alarms");
        $result = $model->field("count(*) as total,FROM_UNIXTIME(`created_time`,'%H') as h")->where($where)->group('h')->select();;
        return $result;
    }

    /**
     * 通过id获取警报信息
     */
    public function getAlarmsInfoById($id, $files = '*')
    {
        $model = D("Usap/Common/Alarms");
        return $model->field($files)->where(['alarm_id' => $id])->find();
    }

    /**
     * 通过$codeserial获取警报信息
     */
    public function UpdateAlarmsInfoByCodeserial($codeserial, $data)
    {
        $model = D("Usap/Common/Alarms");
        return $model->where(['codeserial' => $codeserial, 'status' => 0])->save($data);
    }

    /**
     * 批量更新警报信息
     */
    public function dealAlermsInfoByMore($codeserial, $data)
    {
        $model = D("Usap/Common/Alarms");
        $model->startTrans();
        $result = false;
        foreach ($data as $value) {
            $id = $value['alarm_id'];
            $res = $model->where(['alarm_id' => $id])->save($value);
            if ($res) {
                $result = true;
            } else {
                $result = false;
            }
        }
        if ($result) {
            $model->commit();
            $r = $model->where(['codeserial' => $codeserial, 'status' => 0])->select();
            if (!$r) {
                $data = [
                    'is_alarming' => 0
                ];
                D('Usap/Common/Devices')->where(['codeserial' => $codeserial])->save($data);
            }
            return true;
        } else {
            $model->rollback();
            return false;
        }
    }


    /**
     * 根据设备id获取最新警报信息
     */
    public function getAlarmsInfoByDeviceId($device_id, $files = '*')
    {
        $model = D("Usap/Common/Alarms");
        return $model->field($files)->where(['device_id' => $device_id])->order('created_time desc')->find();
    }


    public function getAlarmsInfoByCodeserial($codeserial, $files = '*')
    {
        $model = D("Usap/Common/Alarms");
        return $model->field($files)->where(['codeserial' => $codeserial])->order('created_time desc')->find();
    }

    /**
     * 更新警报信息
     */
    public function updateAlarmsInfo($id, $data)
    {
        $model = D("Usap/Common/Alarms");
        return $model->where(['alarm_id' => $id])->save($data);
    }

    public function findAlamsInfo($where, $files)
    {
        $model = D("Usap/Common/Alarms");
        $res = $model->alias('a')->field($files)->join('left join devices as b on a.device_id = b.device_id')->where($where)->select();
//        dprint($model->getLastSql());
        return $res;
    }

}